# vim: fileencoding=utf-8
#!-*- coding:utf-8 -*-
#日本語
import cgi
import logging
import datetime
from django.utils import simplejson
from google.appengine.ext import db,webapp
from google.appengine.api import memcache
def setMemCacheData(user,category,year,month,price):
    accountData=None
    cacheName=user.nickname()+str(year)+str(month)+"sum"
    accounts = memcache.get(cacheName)
    if accounts is not None:
      accountData = simplejson.loads(accounts)
      if category.name in accountData:
        accountData[category.name] += price
      else:
        accountData[category.name] = price
      memcache.set(cacheName, simplejson.dumps(accountData))
def appendMonth(year,month,idx):
  aY=None 
  if idx < 0:
    abs_idx = idx * -1
    aY = abs_idx/12
    aY *= -1
    aM = abs_idx%12
    aM = aM * -1
  else:
    aY=idx/12
    aM = idx%12
  nMonth = month + aM 
  if nMonth > 12:
     aY += 1
     nMonth -= 12
  elif nMonth < 0:
     aY -= 1
     nMonth = 12 + nMonth
  return {'year':year+aY,'month':nMonth}
def setUserMonthAccount(user,year,month):
    day = 1
    fromdate = datetime.datetime(year,month,day)
    next = appendMonth(year,month,1)
    todate = datetime.datetime(next["year"],next["month"],day)
    datas = Spending.all().filter('user',user).filter('date >=',fromdate).filter('date < ',todate)
    accountData = {}
    for data in datas:
        if data.category.name in accountData:
            accountData[data.category.name] = accountData[data.category.name] + data.price
        else:
            accountData[data.category.name] = data.price
    memcache.add(user.nickname()+str(year)+str(month)+"sum", simplejson.dumps(accountData))
    return accountData

class Category(db.Model):
  name = db.StringProperty()
  user = db.UserProperty()
  position = db.IntegerProperty()

class DayReserve(db.Model):
  day = db.IntegerProperty()
  user = db.UserProperty()
  price = db.IntegerProperty()
  category = db.ReferenceProperty(Category)
  content = db.StringProperty()
  last_date = db.DateTimeProperty()

class Spending(db.Model):
  price = db.IntegerProperty()
  category = db.ReferenceProperty(Category)
  content = db.StringProperty()
  date = db.DateTimeProperty()
  user = db.UserProperty()
		
class Daymemo(db.Model):
    memo = db.TextProperty()
    date = db.DateTimeProperty()
    user = db.UserProperty()

def add_account(self,user):
  content = self.request.get("content")
  price = int(self.request.get("price"))
  year= int(self.request.get("account_year"))
  month= int(self.request.get("account_month"))
  day = int(self.request.get("account_day"))
  date = datetime.datetime(year,month,day)
  category = self.request.get("category")
  spending = Spending(user=user,
                    content=content,
                    price = price,
                    date = date)
  spending.category=db.Key(category)
  spending.put()
  setMemCacheData(user,spending.category,year,month,spending.price)
